package decotor

import (
	"errors"
	"fmt"
	"log"
)

// 订单结构
type Order struct {
	ID     string
	Amount float64
	Items  []string
}

// 组件接口
type OrderHandler interface {
	Handle(order Order) error
}

type BasicOrderHandler struct {
}

func (r *BasicOrderHandler) Handle(order Order) error {
	fmt.Printf("处理基础订单：%s, 金额：%.2f\n", order.ID, order.Amount)
	return nil
}

// 参数校验装饰器
type ValidationDecorator struct {
	handler OrderHandler
}

func (r *ValidationDecorator) Handle(order Order) error {
	fmt.Println("开始校验订单参数...")
	if len(order.ID) == 0 {
		return errors.New("订单ID不能为空")
	}
	if order.Amount <= 0 {
		return errors.New("订单金额必须大于0")
	}
	return r.handler.Handle(order)
}

// 日志装饰器
type LoggingDecorator struct {
	handler OrderHandler
}

func (r *LoggingDecorator) Handle(order Order) error {
	log.Printf("开始处理订单：%s", order.ID)
	err := r.handler.Handle(order)
	if err != nil {
		log.Printf("订单处理失败：%v", err)
	} else {
		log.Printf("订单处理成功：%s", order.ID)
	}
	return err
}

// 加密装饰器
type EncryptionDecorator struct {
	handler OrderHandler
}

func (e *EncryptionDecorator) Handle(order Order) error {
	fmt.Println("加密订单敏感数据...")
	// 这里可以添加实际的加密逻辑
	return e.handler.Handle(order)
}
